/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');


    app.useModule("ui.table");
    var toastr =require('toastr');
    var datepicker = require('datepicker');
    app.controller('coupon_listCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.orderParams = {
            url: "/admin/coupon/list"
        };
        datepicker($scope);
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };

        //创建
        $scope.addOrderModal=function(){
            $scope.addModal = !$scope.addModal ;
            $scope.add={};
        };

        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDate=$scope.ctrlScope.itemsearch.beginDatex;
            $scope.ctrlScope.itemsearch.endDate= $scope.ctrlScope.itemsearch.endDatex;
            $scope.ctrlScope.itemsearch.beginDate=$filter('date')($scope.ctrlScope.itemsearch.beginDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDate=$filter('date')($scope.ctrlScope.itemsearch.endDate,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };
        //新建提交
        $scope.saveSubmit=function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            var parm=$scope.add;
            console.info($scope.add);
            parm.expiredTime=$filter('date')(parm.expiredTime,'yyyy-MM-dd');
            //parm.userId=1;
            //parm.couponType=1;
            $http({
                method: 'POST',
                url: "/admin/coupon/add",
                data:parm,
            }).success(function(data) {

                if(data.result=='fail'){
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('创建失败');
                    }

                }else{
                    toastr.success("创建成功！");
                    $scope.addModal = !$scope.addModal ;
                    $scope.orderParams.reload();
                }

            }).error(function(data) {
            });
        };
    }]);


});